﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpWeb
{
    /// <summary>
    /// 表单元素的前台数据验证设置
    /// </summary>
    public class ValidationSetting
    {
        private bool _Required = false;
        /// <summary>
        /// 标记为必填
        /// </summary>
        public bool Required
        {
            get { return _Required; }
            set { _Required = value; }
        }
        private bool _Creditcard = false;
        /// <summary>
        /// 标记为信用卡
        /// </summary>
        public bool Creditcard
        {
            get { return _Creditcard; }
            set { _Creditcard = value; }
        }

        private bool _Email = false;
        /// <summary>
        /// 标记为邮箱地址
        /// </summary>
        public bool Email
        {
            get { return _Email; }
            set { _Email = value; }
        }
        private bool _Url = false;
        /// <summary>
        /// 标记为网址
        /// </summary>
        public bool Url
        {
            get { return _Url; }
            set { _Url = value; }
        }

        private bool _Phone = false;
        /// <summary>
        /// 标记为电话号码
        /// </summary>
        public bool Phone
        {
            get { return _Phone; }
            set { _Phone = value; }
        }
        private bool _Digits = false;
        /// <summary>
        /// 标记为整数
        /// </summary>
        public bool Digits
        {
            get { return _Digits; }
            set { _Digits = value; }
        }
        private bool _Number;
        /// <summary>
        /// 标记为浮点数
        /// </summary>
        public bool Number
        {
            get { return _Number; }
            set { _Number = value; }
        }
        private bool _Lettersonly;
        /// <summary>
        /// 标记为字母
        /// </summary>
        public bool LettersOnly
        {
            get { return _Lettersonly; }
            set { _Lettersonly = value; }
        }


        private bool _Alphanumeric = false;
        /// <summary>
        /// 只能为字母、数字或下划线
        /// </summary>
        public bool Alphanumeric
        {
            get { return _Alphanumeric; }
            set { _Alphanumeric = value; }
        }

        private int? _Min = null;
        /// <summary>
        /// 指定最小值
        /// </summary>
        public int? Min
        {
            get { return _Min; }
            set { _Min = value; }
        }

        private int? _Max = null;
        /// <summary>
        /// 指定最大值
        /// </summary>
        public int? Max
        {
            get { return _Max; }
            set { _Max = value; }
        }

        private int? _MinLength = null;
        /// <summary>
        /// 最小长度
        /// </summary>
        public int? MinLength
        {
            get { return _MinLength; }
            set { _MinLength = value; }
        }

        private int? _MaxLength = null;
        /// <summary>
        /// 最大长度
        /// </summary>
        public int? MaxLength
        {
            get { return _MaxLength; }
            set { _MaxLength = value; }
        }

        private string _EqualTo = null;
        /// <summary>
        /// 指定与另一个元素相等，此值指向该元素的Id。
        /// e.g:"#psw1"
        /// </summary>
        public string EqualTo
        {
            get { return _EqualTo; }
            set { _EqualTo = value; }
        }

        private string _Remote = null;
        /// <summary>
        /// 指定后台验证的位置
        /// </summary>
        public string Remote
        {
            get { return _Remote; }
            set { _Remote = value; }
        }
        /// <summary>
        /// 将设置应用到标签
        /// </summary>
        /// <param name="tag"></param>
        public virtual void Apply(TagComponent tag)
        {
            if (Creditcard)
                tag.Attributes.Class.AddClass("creditcard");
            if (Alphanumeric)
                tag.Attributes.Class.AddClass("Alphanumeric");
            if (Digits)
                tag.Attributes.Class.AddClass("digits");
            if (Email)
                tag.Attributes.Class.AddClass("email");
            if (EqualTo != null)
                tag.Attributes["equalto"] = this.EqualTo;
            if (LettersOnly)
                tag.Attributes.Class.AddClass("lettersonly");
            if (Max.HasValue)
                tag.Attributes["max"] = this.Max.ToString();
            if (Min.HasValue)
                tag.Attributes["min"] = this.Min.ToString();
            if (MaxLength.HasValue)
                tag.Attributes["maxlength"] = this.MaxLength.ToString();
            if (MinLength.HasValue)
                tag.Attributes["minlength"] = this.MinLength.ToString();
            if (Number)
                tag.Attributes.Class.AddClass("number");
            if (Phone)
                tag.Attributes.Class.AddClass("phone");
            if (!string.IsNullOrEmpty(Remote))
                tag.Attributes["remote"] = this.Remote.ToString();
            if (Required)
                tag.Attributes.Class.AddClass("required");
            if (Url)
                tag.Attributes.Class.AddClass("url");
        }
    }
}
